
import os
import django
from django.conf import settings

if not settings.configured:
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'matrix.settings')  # 添加了环境
    django.setup()  # 初始化 Django 环境


from serverworker.models import electricity_meter,Temperature_humidity
import socket






def encode_u(data):
    if data.startswith('0f03'):
        num=round(int(data[6:-4],16 )*0.1,2)
        print(num)
        return num
    else:
        print('data err')
def encode_i(data):
    if data.startswith('0f03'):
        num=int(data[6:-4],16 )*0.01
        print(num)
        return num
    else:
        print('data err')
def encode_q(data):
    print(data)
    if data.startswith('0f03'):
        num=round(int(data[6:-4],16 )*0.001,2)
        print(num)
        return num
    else:
        print('data err')
def encode_p(data):
    print(data)
    if data.startswith('0f03'):
        num=round(int(data[6:14],16 )*0.01,2)
        print(num)
        return num
    else:
        print('data err')
def encode_temps(data):
    if data.startswith('0104'):
        num=round(int(data[6:-4],16 )*0.1,2)
        print(num)
        return num
    else:
        print('data err')
def encode_sd(data):
    if data.startswith('0104'):
        num=round(int(data[6:-4],16 )*0.1,2)
        print(num)
        return num
    else:
        print('data err')
dict_code = {"u":'0F 03 03 00 00 01 85 60',
            "i":'0F 03 03 03 00 01 75 60',
            "q":'0F 03 03 12 00 01 25 65',
            "w":'0F 03 00 00 00 04 45 27'}

def iot_tastek_power(host='192.168.0.80', port=8081):
    for i in dict_code:
        print()
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
            s.connect((host, port))
            s.sendall(bytes.fromhex(dict_code[i]))
            data1 = s.recv(1024).hex()
            encode_u(data1)

def oit_tastek_1(host='192.168.0.80', port=8081):
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.connect((host, port))
        s.sendall(bytes.fromhex('0F 03 03 00 00 01 85 60'))#电压
        data1 = encode_u(s.recv(1024).hex())

        s.sendall(bytes.fromhex('0F 03 03 03 00 01 75 60'))#电流
        data2 = encode_i(s.recv(1024).hex())

        s.sendall(bytes.fromhex('0F 03 03 12 00 01 25 65'))#功率因数
        data3 = encode_q(s.recv(1024).hex())

        s.sendall(bytes.fromhex('0F 03 00 00 00 04 45 27'))#电量
        data4 = encode_p(s.recv(1024).hex())
    if data1 and data2 and data3 and data4:
        return (data1, data2, data3, data4)
    else:
        return False

def oit_tastek_2(host='192.168.0.80', port=10125):
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.connect((host, port))
        s.sendall(bytes.fromhex('01 04 00 00 00 01 31 CA'))
        data = s.recv(1024).hex()
        tmps = encode_temps(data)
        s.sendall(bytes.fromhex('01 04 00 01 00 01 60 0A'))
        data = s.recv(1024).hex()
        sd = encode_sd(data)
    if tmps and sd:
        return (tmps,sd)
    else:
        return False


def run():
    res1 = oit_tastek_1()
    res2 = oit_tastek_2()
    if res1:
        electricity_meter.objects.create(u=res1[0],i=res1[1],q=res1[2],w=res1[3])
    if res2:
        Temperature_humidity.objects.create(temperature=res2[0], humidity=res2[1])

if __name__ == "__main__":
    run()